<!--
 * @Author: wangzhichiao<https://github.com/wzc570738205>
 * @Date: 2020-05-26 09:44:28
 * @LastEditors: wangzhichiao<https://github.com/wzc570738205>
 * @LastEditTime: 2020-05-28 11:19:41
 -->
<template>
  <el-dialog
    :title="title"
    :visible="visible"
    :append-to-body="true"
    @close="visible = false"
    width="600px"
  >
    <form-create v-model="fApi" :rule="data" :option="option"></form-create>
    <div class="btn">
      <ElButton type="primary" plain @click="submit">提交</ElButton>
      <ElButton aligen="center" plain @click="reset">重置</ElButton>
    </div>
    <div slot="footer">
      <el-button @click="visible = false">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      required: true,
      default: () => ''
    },
    show: {
      type: Boolean,
      required: true,
      default: () => false
    },
    data: {
      type: Object,
      required: false,
      default: () => {}
    }
  },
  data() {
    return {
      visible: false,
      // 实例对象
      fApi: {},
      // 表单生成规则

      // 组件参数配置
      option: {
        // 表单提交事件
        submitBtn: false,
        onSubmit: function(formData) {
          alert(JSON.stringify(formData))
        }
      }
    }
  },
  methods: {
    submit() {
      console.log(this.fApi)
      this.fApi.submit((formData, $f) => {
        alert(JSON.stringify(formData))
      })
    },
    reset() {
      this.fApi.resetFields()
    },
    fileonSuccess(e) {
      console.log(e)
    }
  },
  mounted() {},
  watch: {
    data: {
      handler: function(newValue, oldValue) {
        newValue.forEach(e => {
          if (e.type === 'upload') {
            console.log(newValue)
            console.log(this.fApi)
            if (this.fApi.updateRule) {
              let props = JSON.parse(JSON.stringify(e.props))
              let field = e.field
              this.fApi.updateRule(
                field,
                {
                  props: {
                    ...props,
                    onSuccess: function(a, b) {
                      console.log(a)
                      console.log(b)
                      // file.url = res.data.filePath;
                    },
                    onError: function(a, b) {
                      console.log(a)
                      console.log(b)
                    }
                  }
                },
                true
              )
            }
          }
        })

        this.visible = true
      },
      deep: false
    }
  }
}
</script>

<style lang="stylus" scoped>
.form
  margin 0 30px
  .form-item
    width 300px
.btn
  display flex
  justify-content center
</style>
